package com.eff.android.fragment.options;

import com.eff.android.R;
import com.eff.android.api.enums.Level;
import com.eff.android.api.enums.Type;
import com.eff.android.api.model.Setting;
import com.eff.android.fragment.EBaseFragment;
import com.eff.android.fragment.IFragmentCallback;
import com.eff.android.lib.ui.EButton;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;

public class EMainFragmentOptions extends EBaseFragment{
	public static final String TAG = EMainFragmentOptions.class.getSimpleName();
	
	EMainFragmentOptionsSubjects eMainOptionsSubjects;
	EButton btnType;
	EButton btnSubjects;
	EButton btnLevel;
	Setting settings;
	
	private IFragmentCallback callback;
	
	public static EMainFragmentOptions newInstance(Bundle b) {
		EMainFragmentOptions f = new EMainFragmentOptions();
		if (b != null){
		    f.setArguments(b);
		}
		return f;
	}

	public static Bundle getArgs() {
		Bundle b = new Bundle();
		b.putString(TAG, TAG);
		return b;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		settings = (Setting) this.getArguments().getSerializable(Setting.TAG);
		Log.d("Setting in EMainFragment", settings.getSubject().name());
		initFragment();
	}
	
	private void initFragment(){
		eMainOptionsSubjects = EMainFragmentOptionsSubjects.newInstance(this.getArguments());
	}

	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
	    Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);
		
		View v = inflater.inflate(R.layout.e_frm_main_options, container, false);
		initViews(v);
		
		return v;
    }
	
	public void ButtonHandle(View target){
		switch(target.getId())
		{
			case R.id.btnSubjects:
				getCallback().gotoNextPage(eMainOptionsSubjects);
				break;
			case R.id.btnType:
				if (btnType.getText() == Type.Word.toString())
				{
					Log.d("Setting subject", settings.getSubject().name());
					settings.setType(Type.Phrase);
					btnType.setText(Type.Phrase.toString());
				}
				else{
					settings.setType(Type.Word);
					btnType.setText(Type.Word.toString());
				}
				break;
			case R.id.btnLevel:
				if (btnLevel.getText() == Level.Easy.toString())
				{
					Log.d("Setting subject", settings.getSubject().name());
					settings.setLevel(Level.Normal);
					btnLevel.setText(Level.Normal.toString());
				}
				else if (btnLevel.getText() == Level.Normal.toString()){
					settings.setLevel(Level.Hard);
					btnLevel.setText(Level.Hard.toString());
				}
				else{
					settings.setLevel(Level.Easy);
					btnLevel.setText(Level.Easy.toString());
				}
				break;
		}
	}

	private void initViews(View v) {
		btnSubjects = (EButton)v.findViewById(R.id.btnSubjects);
		btnSubjects.setText(settings.getSubject().name());
		btnType = (EButton) v.findViewById(R.id.btnType);
		btnType.setText(settings.getType().name());
		btnLevel = (EButton)v.findViewById(R.id.btnLevel);
		btnLevel.setText(settings.getLevel().name());
		
		
		btnSubjects.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ButtonHandle(v);
			}
		});
		btnType.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ButtonHandle(v);
			}
		});
		btnLevel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ButtonHandle(v);
			}
		});
	}

	public IFragmentCallback getCallback() {
		return callback;
	}

	public void setCallback(IFragmentCallback callback) {
		this.callback = callback;
	}
}
